/**
 * ApplianceSetup.java
 * 
 * Loads appliance data from a text file and handles appliance related requests.
 */

package com.swin.energy;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.swin.energy.DataGenerator.SEASON;

// TODO: Add support for influential factors
public class ApplianceSetup {

	private String APPLICATION_FILE = "appliances.txt";

	private Map<String, Appliance> appliances;

	public ApplianceSetup() {
		appliances = new HashMap<String, Appliance>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					APPLICATION_FILE));
			String line;
			while ((line = reader.readLine()) != null) {
				String[] values = line.split(",");
				appliances.put(values[0], new Appliance(values));
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public Double getApplianceRate(String applianceName) {
		return appliances.get(applianceName).getRate();
	}
	
	public boolean isApplianceTurnedOn(String name, int hour, SEASON season) {
		if (Math.random() < appliances.get(name).getProbability(season, hour))
			return true;
		else
			return false;
	}
	
	public double getApplianceNumber(String appName) {
		switch(appName) {
		case "fridge": return 0.0;
		case "tv": return 1.0;
		case "pc": return 2.0;
		case "heater": return 3.0;
		case "dryer": return 4.0;
		}
		return -1.0;
	}

}
